import {setHtmlVersion, setMenus, setRefreshToken, setRoleInfo, setToken, setUserInfo} from "@/utils/storage";
import $F from "@/utils/kit";

/**
 * 舒心用户信息
 */
export const refreshUserInfo = (res: any) => {
    if (res.data.token) {
        setToken(res.data.token);
    }
    if (res.data.version) {
        setHtmlVersion(res.data.version);
    }
    if (res.data.refreshToken) {
        setRefreshToken(res.data.refreshToken);
    }
    if (res.data.userInfo) {
        setUserInfo(JSON.stringify(res.data.userInfo));
    }
    if (res.data.roleInfo) {
        setRoleInfo(JSON.stringify(res.data.roleInfo));
    }
    refreshMenu(res.data.menus);
};

/**
 * 刷新用户菜单
 */
export const refreshMenu = (menus: any) => {
    if (menus) {
        let mMenus: any = [];
        for (let m of menus) {
            let menu = JSON.parse(JSON.stringify(m)) as any;
            menu.meta = {};
            if (menu.hidden) {
                menu.meta.hidden = menu.hidden;
                delete menu["hidden"];
            }
            if (menu.title) {
                menu.meta.title = menu.title;
                delete menu["title"];
            }
            if (menu.icon) {
                menu.meta.icon = menu.icon;
                delete menu["icon"];
            }
            if (menu.alwaysShow) {
                menu.meta.alwaysShow = menu.alwaysShow;
                delete menu["alwaysShow"];
            }
            if (menu.type) {
                menu.meta.type = menu.type;
                delete menu["type"];
            }
            if (menu.affix) {
                menu.meta.affix = menu.affix;
                delete menu["affix"];
            }
            delete menu["sort"];
            delete menu["createUid"];
            delete menu["createTime"];
            delete menu["updateUid"];
            delete menu["updateTime"];
            delete menu["dataStates"];
            if (!menu.redirect) {
                delete menu["redirect"];
            }
            mMenus.push(menu);
        }
        setMenus(JSON.stringify($F.tree(mMenus as any)));
    }
};
